package cn.zzmx.controller;



import cn.zzmx.entity.Product;
import cn.zzmx.entity.ResultMsg;
import cn.zzmx.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @RequestMapping("/queryProductAll")
    public ResultMsg<Product> queryProductAll(@Param("page") Integer page, @Param("limit") Integer limit){
        ResultMsg<Product> msg = new ResultMsg<>();
        PageInfo<Product> productPageInfo = iProductService.queryProductAll(page, limit);

        msg.setCode(0);
        msg.setCount(productPageInfo.getTotal());
        msg.setData(productPageInfo.getList());
        msg.setMsg("查询成功");

        return msg;
    }


    @RequestMapping("/deleteProducts")
    public ResultMsg<Product> deleteProducts(@Param("ids") String [] ids){
        iProductService.deleteProducts(ids);
        ResultMsg<Product> msg = new ResultMsg<>();
        msg.setCode(0);
        msg.setMsg("删除成功");
        return msg;

    }

    @RequestMapping("/deleteProductById")
    public ResultMsg<Product> deleteProductById(@Param("id") String id){
        iProductService.deleteProductById(id);
        ResultMsg<Product> msg = new ResultMsg<>();
        msg.setCode(0);
        msg.setMsg("删除成功");
        return msg;


    }


    @RequestMapping("/addProduct")
    public ResultMsg<Product> addProduct(@RequestBody Product product){

        iProductService.addProduct(product);
        ResultMsg<Product> msg = new ResultMsg<>();
        msg.setCode(0);
        msg.setMsg("添加成功");

        return msg;
    }

    @RequestMapping("/queryProductById")
    public ResultMsg<Product> queryProductById(@Param("id") String id){
        List<Product> products = iProductService.queryProductById(id);
        ResultMsg<Product> msg = new ResultMsg<>();
        msg.setData(products);
        msg.setCode(0);
        msg.setMsg("查询成功");

        return msg;
    }

    @RequestMapping("/updateProduct")
    public ResultMsg<Product> updateProduct(@RequestBody Product product){
        iProductService.updateProduct(product);
        ResultMsg<Product> msg = new ResultMsg<>();
        msg.setCode(0);
        msg.setMsg("更新成功");

        return msg;
    }

}
